<template>
  <el-dialog :title="title" :visible="showDialog" width="50%" @close="close">
    <el-form ref="formRef" :rules="rules" :model="formData" label-width="80px">
      <el-form-item label="部门名称" label-width="120px" prop="name">
        <el-input v-model="formData.name" placeholder="" />
      </el-form-item>
      <el-form-item label="部门编码" label-width="120px" prop="code">
        <el-input v-model="formData.code" placeholder="" />
      </el-form-item>
      <el-form-item label="部门负责人" label-width="120px">
        <el-select v-model="formData.manager" placeholder="请选择负责人" @focus="getEmployeeSimple">
          <el-option
            v-for="itm in userList"
            :key="itm.id"
            :label="itm.username"
            :value="itm.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" label-width="120px" prop="introduce">
        <el-input v-model="formData.introduce" placeholder="" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="center">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="close">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>

import { getDeaprts, addDeapty, seeDepart, updataDeapty } from '@/api/department'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    dataClick: {
      type: Object,
      default: null
    }
  },
  data() {
    var checkName = async(rule, value, callback) => {
      const { depts } = await getDeaprts()
      var flag = false
      if (this.formData.id) {
        flag = depts
          .filter(itm => itm.id !== this.dataClick.id && itm.pid === this.dataClick.pid)
          .some(itm => itm.name === value.trim())
      } else {
        flag = depts
          .filter(itm => itm.pid === this.dataClick.id)
          .some(itm => itm.name === value.trim())
      }
      if (flag) {
        callback(new Error('同级部门中禁止出现重复部门'))
      } else {
        callback()
      }
    }
    var checkCode = async(rule, value, callback) => {
      const { depts } = await getDeaprts()
      var flag = false
      if (this.formData.id) {
        flag = depts.filter(itm => !itm.id === this.dataClick.id).some(itm => itm.code === value.trim())
      } else {
        flag = depts.some(itm => itm.code === value.trim())
      }
      if (flag) {
        callback(new Error('部门编码在整个模块中都不允许重复'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      userList: [],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '部门名称必填' },
          { max: 50, min: 1, trigger: 'blur', message: '部门名称必填' },
          { validator: checkName, trigger: 'blur' }],
        code: [{ required: true, trigger: 'blur', message: '部门编码必填' },
          { max: 50, min: 1, trigger: 'blur', message: '部门名称必填' },
          { validator: checkCode, trigger: 'input' }],
        manager: [{ required: true, trigger: 'blur', message: '部门负责人必填' }],
        introduce: [{ required: true, trigger: 'blur', message: '部门介绍必填' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    async  getEmployeeSimple() {
      const data = await getEmployeeSimple()
      this.userList = data
    },
    // 关闭
    close() {
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:showDialog', false)
    },
    // 编辑
    async seeDepart(id) {
      this.formData = await seeDepart(id)
    },
    // 提交
    async submit() {
      try {
        await this.$refs.formRef.validate()
        if (this.formData.id) {
          await updataDeapty(this.formData)
        } else {
          await addDeapty({ ...this.formData, pid: this.dataClick.id })
        }
        this.$emit('update')
        this.$message.success(this.formData.id ? '更新成功' : '添加部门成功')
        this.close()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
